var lib = new Object();

window.onload = function(){
	load("xml/country.xml", countriesLoaded, true, false, true, true);
};

var countriesLoaded = function(src){
	lib.countries = new Array();
	var countryNodes = xPath(src, "country/c");
	for(var t=0; t<countryNodes.length; t++){
		lib.countries[t] = xml2str(countryNodes[t].firstChild);
	}
};

var autoSuggest = function(field){
	var checkValue = function(){
		autoSuggestRender(field);
	};
	lib.metroSuggest = setInterval(checkValue, 200);
	field.onblur = function(){
		clearInterval(lib.metroSuggest);
		if(obj("autoSuggestLayer")) obj("autoSuggestLayer").parentNode.removeChild(obj("autoSuggestLayer"));
	};
};

var autoSuggestRender = function(field){
	var sub = field.value;
	var t=0;
	var result = new Array();
	if(sub.length > 0){
		while(t<lib.countries.length && result.length <= 7){
			if(lib.countries[t].substr(0, sub.length).toLowerCase() == sub.toLowerCase()) result.push(lib.countries[t]);
			t++;
		}
	}
	var sDiv = obj("autoSuggestLayer");
	if(!sDiv){
		sDiv = document.createElement("div");
		sDiv.setAttribute("id", "autoSuggestLayer");
		document.body.appendChild(sDiv);
	}
	sDiv.style.position = "absolute";
	var fieldPos = findPos(field);
	sDiv.style.top = fieldPos[1] + field.offsetHeight;
	sDiv.style.left = fieldPos[0];
	sDiv.innerHTML = "";
	for(var t=0; t<result.length; t++){
		sDiv.innerHTML += result[t] + "<br/>";
	}
}
